layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',//主入口模块
    treetable: 'treetable-lay/treetable'
}).use(['form']);

layui.use(['table','treetable','layer'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var treetable = layui.treetable;
    var $ = layui.$;

    //渲染表格
    var renderTable = function(){
        treetable.render({
            height: 'full-100',
            id: "menu",
            treeColIndex: 1,	//树形图标显示在第几列
            treeSpid: '0',		//最上级的父级id
            treeIdName: 'id',	//id字段的名称
            treePidName: 'menuPid',	//父级节点字段
            treeDefaultClose: false,	//是否默认折叠
            treeLinkage: false,		//父级展开时是否自动展开所有子级
            elem: '#menu',	//表格id
            url: 'http://localhost:8888/ams-admin/ams/menu/list',
            page: false,
            cols: [ [
                  {type: 'numbers'}
                , {field: 'menuName', width: 200, title: '菜单名称', align: 'center'}
                , {
                    field: 'icon', width: 100, title: '图标', align: 'center', templet: function (item) {
                        return '<i class="' + item.icon + '" aria-hidden="true"></i>';
                    }
                }
                , {
                    field: 'type', width: 100, title: '类型', align: 'center', templet: function (item) {
                        if (item.type === 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        }
                        if (item.type === 1) {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                        if (item.type === 2) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                    }
                }
                , {field: 'path', width: 180, title: '菜单path', align: 'center'}
                , {field: 'perms', width: 200, title: '授权标识', align: 'center'}
                , {fixed: 'right', width: 180, align: 'center', toolbar: '#barDemo', title: '操作'}
            ] ]
        })
    };
    renderTable();

    table.on('tool(menu)', function (obj) {
        var row = obj.data;
        var menuId = row.id;

        if (obj.event === 'del') {
            layer.confirm('确定删除此行么？', function (index) {
                layer.close(index);
                $.ajax({
                    type: "post",
                    url: "http://localhost:8888/ams-admin/ams/menu/delete",
                    data: "menuId=" + menuId,
                    success: function (r) {
                        if (r['code'] === 0) {
                            layer.alert("删除成功!", function () {
                                var ind = parent.layer.getFrameIndex(window.name);
                                layer.close(ind);
                                window.location.reload();
                            });
                        } else {
                            layer.alert(r['msg']);
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: "编辑菜单"
                , closeBtn: 1
                , area: ['800px', '560px']
                , shade: 0.8
                , btnAlign: 'c'
                , content: 'updMenu.html'
                , success: function (layero) {
                    var iframein = window[layero.find('iframe')[0]['name']];

                    if(row.type === 0){
                        iframein.$("#path1").css("display","none");
                        iframein.$("#perms1").css("display","none");
                    }

                    if(row.type === 2){
                        iframein.$("#path1").css("display","none");
                        iframein.$("#icon1").css("display","none");
                    }

                    iframein.$("#id").val(row.id);
                    iframein.$("#type").val(row.type);
                    iframein.$("#menuPid").val(row.menuPid);
                    iframein.$("#parentName").val(row.parentName);
                    iframein.$("#menuName").val(row.menuName);
                    iframein.$("#path").val(row.path);
                    iframein.$("#perms").val(row.perms);
                    iframein.$("#icon").val(row.icon);
                    iframein.layui.form.render();
                }
            });
        }
    });

    $('#addNotice').on('click', function () {
        layer.open({
            type: 2
            ,title: "新增菜单"
            ,closeBtn: 1
            ,area: ['800px', '560px']
            ,shade: 0.8
            ,btnAlign: 'c'
            ,content: 'addMenu.html'
        });
    });

    $('#btn-expand').on('click', function () {
        treetable.expandAll('#menu');
    });

    $('#btn-fold').on('click', function () {
        treetable.foldAll('#menu');
    });

});